package com.sqx.modules.pay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayConstants;
import com.github.wxpay.sdk.WXPayUtil;
import com.google.gson.Gson;
import com.sqx.common.utils.DateUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.dao.UserMoneyDao;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.entity.UserMoneyDetails;
import com.sqx.modules.app.service.UserMoneyDetailsService;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.common.entity.CommonInfo;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.goods.dao.GoodsDao;
import com.sqx.modules.goods.entity.Goods;
import com.sqx.modules.pay.config.WXConfig;
import com.sqx.modules.pay.dao.PayDetailsDao;
import com.sqx.modules.pay.entity.PayDetails;
import com.sqx.modules.pay.service.WxService;
import com.sqx.modules.sign.dao.GoldRecordsDao;
import com.sqx.modules.sign.entity.GoldRecords;
import com.sqx.modules.task.dao.TbDailyTasksDao;
import com.sqx.modules.task.dao.TbUserTaskDao;
import com.sqx.modules.task.entity.TbDailyTasks;
import com.sqx.modules.task.entity.TbUserTask;
import com.sqx.modules.tbOrders.dao.TbOrdersDao;
import com.sqx.modules.tbOrders.entity.TbOrders;
import com.sqx.modules.tbOrders.service.TbOrdersService;
import com.sqx.modules.utils.AmountCalUtils;
import com.sqx.modules.utils.MD5Util;
import com.sqx.modules.utils.WXConfigUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author fang
 * @date 2020/2/26
 */
@Service
@Slf4j
public class WxServiceImpl implements WxService {
    private static final String SPBILL_CREATE_IP = "127.0.0.1";
    private static final String TRADE_TYPE_APP = "APP";
    private static final String TRADE_TYPE_NATIVE = "NATIVE";
    private static final String TRADE_TYPE_JSAPI = "JSAPI";
    private static final String TRADE_TYPE_MWEB = "MWEB";

    @Autowired
    private CommonInfoService commonInfoService;
    @Autowired
    private UserService userService;
    @Autowired
    private PayDetailsDao payDetailsDao;
    @Autowired
    private TbOrdersService tbOrdersService;
    @Autowired
    private UserMoneyDetailsService userMoneyDetailsService;

    @Autowired
    private UserMoneyDao userMoneyDao;

    @Autowired
    private TbOrdersDao tbOrdersDao;

    @Autowired
    private TbUserTaskDao tbUserTaskDao;

    @Autowired
    private TbDailyTasksDao tbDailyTasksDao;

    @Autowired
    private TbOrdersService orderService;

    @Autowired
    private GoldRecordsDao goldRecordsDao;
    @Autowired
    private GoodsDao goodsDao;

    private ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock(true);

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 微信支付订单生成
     *
     * @param moneys     支付金额 带小数点
     * @param type       类型 7 微信H5支付
     * @param userId     用户id
     * @param outTradeNo 单号
     * @return
     * @throws Exception
     */
    private Result payH5(Double moneys, Integer type, Long userId, String outTradeNo) throws Exception {
        //h5服务域名配置
        CommonInfo oneu = commonInfoService.findOne(19);
        String url = null;
        url = oneu.getValue() + "/sqx_fast/app/wxPay/notifyJsApi";
        String currentTimeMillis = (System.currentTimeMillis() / 1000) + "";
        //后台服务名称
        CommonInfo one = commonInfoService.findOne(12);
        log.info("回调地址：" + url);
        Double mul = AmountCalUtils.mul(moneys, 100);
        String money = String.valueOf(mul.intValue());
        String generateNonceStr = WXPayUtil.generateNonceStr();
        WXConfig config = new WXConfig();
        //微信小程序APPID        微信公众号APPID
        config.setAppId(commonInfoService.findOne(45).getValue());
        //微信商户key
        config.setKey(commonInfoService.findOne(75).getValue());
        //微信商户号mchId
        config.setMchId(commonInfoService.findOne(76).getValue());
        WXPay wxpay = new WXPay(config);
        Map<String, String> data = new HashMap<>();
        data.put("appid", config.getAppID());
        data.put("mch_id", config.getMchID());
        data.put("nonce_str", generateNonceStr);
        String body = one.getValue();
        data.put("body", body);
        //生成商户订单号，不可重复
        data.put("out_trade_no", outTradeNo);
        data.put("total_fee", money);
        //自己的服务器IP地址
        data.put("spbill_create_ip", SPBILL_CREATE_IP);
        //异步通知地址（请注意必须是外网）
        data.put("notify_url", url);
        //交易类型
        data.put("trade_type", TRADE_TYPE_MWEB);
        Map<String, Object> sceneInfo = new HashMap<>();
        Map<String, String> h5Info = new HashMap<>();
        h5Info.put("type", "Wap");
        h5Info.put("wap_url", "https://xianyu.xianmaxiong.com"); // 网站URL
        h5Info.put("wap_name", "咸鱼二手交易"); // 商品名称
        sceneInfo.put("h5_info", h5Info);
        data.put("scene_info", new Gson().toJson(sceneInfo));
        //附加数据，在查询API和支付通知中原样返回，该字段主要用于商户携带订单的自定义数据
        data.put("attach", "");
        data.put("sign", WXPayUtil.generateSignature(data, config.getKey(),
                WXPayConstants.SignType.MD5));
        UserEntity userEntity = userService.queryByUserId(userId);
        if (StringUtils.isNotBlank(userEntity.getOpenId())) {
            data.put("openid", userEntity.getOpenId());
        }
        //使用官方API请求预付订单
        Map<String, String> response = wxpay.unifiedOrder(data);
        for (String key : response.keySet()) {
            log.info("微信支付订单微信返回参数：keys:" + key + "    value:" + response.get(key).toString());
        }
        if ("SUCCESS".equals(response.get("return_code"))) {//主要返回以下5个参数
                Map<String, String> param = new HashMap<>();
                param.put("appid", config.getAppID());
                param.put("partnerid", response.get("mch_id"));
                param.put("prepayid", response.get("prepay_id"));
                param.put("noncestr", generateNonceStr);
                param.put("timestamp", currentTimeMillis);
                    /*param.put("sign", WXPayUtil.generateSignature(param, config.getKey(),
                            WXPayConstants.SignType.MD5));*/
                String stringSignTemp = "appId=" + config.getAppID() + "&nonceStr=" + generateNonceStr + "&package=prepay_id=" + response.get("prepay_id") + "&signType=MD5&timeStamp=" + currentTimeMillis + "" + "&key=" + config.getKey();
                String sign = MD5Util.md5Encrypt32Upper(stringSignTemp).toUpperCase();
                param.put("sign", sign);
                param.put("outtradeno", outTradeNo);
                param.put("package", "prepay_id=" + response.get("prepay_id"));//给前端返回的值
                param.put("mweb_url", response.get("mweb_url"));
                param.put("trade_type", response.get("trade_type"));
                param.put("return_msg", response.get("return_msg"));
                param.put("result_code", response.get("result_code"));
                param.put("signType", "MD5");
                return Result.success().put("data", param);
        }
        return Result.error("获取订单失败");
    }

    /**
     * 微信支付订单生成
     *
     * @param moneys     支付金额 带小数点
     * @param type       类型 1app  2 二维码支付  3小程序 公众号支付
     * @param userId     用户id
     * @param outTradeNo 单号
     * @return
     * @throws Exception
     */
    private Result pay(Double moneys, Integer type, Long userId, String outTradeNo) throws Exception {
        //h5服务域名配置
        CommonInfo oneu = commonInfoService.findOne(19);
        String url;
        if (type == 3) {
            url = oneu.getValue() + "/sqx_fast/app/wxPay/notifyJsApi";
        } else if (type == 2) {
            url = oneu.getValue() + "/sqx_fast/app/wxPay/notifyMp";
        } else {
            url = oneu.getValue() + "/sqx_fast/app/wxPay/notify";
        }
        String currentTimeMillis = (System.currentTimeMillis() / 1000) + "";
        //后台服务名称
        CommonInfo one = commonInfoService.findOne(12);
        log.info("回调地址：" + url);
        Double mul = AmountCalUtils.mul(moneys, 100);
        String money = String.valueOf(mul.intValue());
        String generateNonceStr = WXPayUtil.generateNonceStr();
        WXConfig config = new WXConfig();
        //微信小程序APPID        微信公众号APPID
        if (type == 1) {
            config.setAppId(commonInfoService.findOne(74).getValue());
        } else if (type == 2) {
            config.setAppId(commonInfoService.findOne(5).getValue());
        } else {
            config.setAppId(commonInfoService.findOne(45).getValue());
        }
        //微信商户key
        config.setKey(commonInfoService.findOne(75).getValue());
        //微信商户号mchId
        config.setMchId(commonInfoService.findOne(76).getValue());
        WXPay wxpay = new WXPay(config);
        Map<String, String> data = new HashMap<>();
        data.put("appid", config.getAppID());
        data.put("mch_id", config.getMchID());
        data.put("nonce_str", generateNonceStr);
        String body = one.getValue();
        data.put("body", body);
        //生成商户订单号，不可重复
        data.put("out_trade_no", outTradeNo);
        data.put("total_fee", money);
        //自己的服务器IP地址
        data.put("spbill_create_ip", SPBILL_CREATE_IP);
        //异步通知地址（请注意必须是外网）
        data.put("notify_url", url);
        //交易类型
        data.put("trade_type", type == 1 ? TRADE_TYPE_APP : TRADE_TYPE_JSAPI);
        //附加数据，在查询API和支付通知中原样返回，该字段主要用于商户携带订单的自定义数据
        data.put("attach", "");
        data.put("sign", WXPayUtil.generateSignature(data, config.getKey(),
                WXPayConstants.SignType.MD5));
        if (type == 3 || type == 2) {
            UserEntity userEntity = userService.queryByUserId(userId);
            if (type == 3) {
                if (StringUtils.isNotBlank(userEntity.getOpenId())) {
                    data.put("openid", userEntity.getOpenId());
                }
            } else {
                data.put("openid", userEntity.getWxOpenId());
            }
        }
        //使用官方API请求预付订单
        Map<String, String> response = wxpay.unifiedOrder(data);
        for (String key : response.keySet()) {
            log.info("微信支付订单微信返回参数：keys:" + key + "    value:" + response.get(key).toString());
        }
        if ("SUCCESS".equals(response.get("return_code"))) {//主要返回以下5个参数
            if (type == 1) {
                Map<String, String> param = new HashMap<>();
                param.put("appid", config.getAppID());
                param.put("partnerid", response.get("mch_id"));
                param.put("prepayid", response.get("prepay_id"));
                param.put("package", "Sign=WXPay");
                param.put("noncestr", generateNonceStr);
                param.put("timestamp", currentTimeMillis);
                param.put("sign", WXPayUtil.generateSignature(param, config.getKey(),
                        WXPayConstants.SignType.MD5));
                param.put("outtradeno", outTradeNo);
                return Result.success().put("data", param);
            } else {
                Map<String, String> param = new HashMap<>();
                param.put("appid", config.getAppID());
                param.put("partnerid", response.get("mch_id"));
                param.put("prepayid", response.get("prepay_id"));
                param.put("noncestr", generateNonceStr);
                param.put("timestamp", currentTimeMillis);
                    /*param.put("sign", WXPayUtil.generateSignature(param, config.getKey(),
                            WXPayConstants.SignType.MD5));*/
                String stringSignTemp = "appId=" + config.getAppID() + "&nonceStr=" + generateNonceStr + "&package=prepay_id=" + response.get("prepay_id") + "&signType=MD5&timeStamp=" + currentTimeMillis + "" + "&key=" + config.getKey();
                String sign = MD5Util.md5Encrypt32Upper(stringSignTemp).toUpperCase();
                param.put("sign", sign);
                param.put("outtradeno", outTradeNo);
                param.put("package", "prepay_id=" + response.get("prepay_id"));//给前端返回的值
                param.put("mweb_url", response.get("mweb_url"));
                param.put("trade_type", response.get("trade_type"));
                param.put("return_msg", response.get("return_msg"));
                param.put("result_code", response.get("result_code"));
                param.put("signType", "MD5");
                return Result.success().put("data", param);
            }
        }
        return Result.error("获取订单失败");
    }

    @Override
    public String payBack(String resXml, Integer type) {
        WXConfig config = null;
        try {
            config = new WXConfig();
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.error("进入回调了！！！");
        if (type == 1) {
            config.setAppId(commonInfoService.findOne(74).getValue());
        } else if (type == 2) {
            config.setAppId(commonInfoService.findOne(5).getValue());
        } else {
            config.setAppId(commonInfoService.findOne(45).getValue());
        }
        config.setKey(commonInfoService.findOne(75).getValue());
        config.setMchId(commonInfoService.findOne(76).getValue());
        WXPay wxpay = new WXPay(config);
        String xmlBack = "";
        Map<String, String> notifyMap = null;
        try {
            notifyMap = WXPayUtil.xmlToMap(resXml);         // 调用官方SDK转换成map类型数据
            if (wxpay.isPayResultNotifySignatureValid(notifyMap)) {//验证签名是否有效，有效则进一步处理
                log.error("验证成功！！！");
                String return_code = notifyMap.get("return_code");//状态
                String out_trade_no = notifyMap.get("out_trade_no");//商户订单号
                if (return_code.equals("SUCCESS")) {
                    log.error("验证成功222！！！");
                    if (out_trade_no != null) {
                        log.error("订单号！！！" + out_trade_no);
                        reentrantReadWriteLock.writeLock().lock();
                        try {
                            orderService.payBack(out_trade_no);
                            PayDetails payDetails = payDetailsDao.selectByOrderId(out_trade_no);
                            TbOrders tbOrders = tbOrdersDao.selectOne(new QueryWrapper<TbOrders>().eq("order_number", out_trade_no));
                            if (tbOrders != null && tbOrders.getTaskId() != null) {
                                TbUserTask task = new TbUserTask();
                                task.setUserId(tbOrders.getBuyerId());
                                task.setTaskId(tbOrders.getTaskId());
                                task.setState(1);
                                task.setRewardClaimed(0);
                                task.setCreateTime(new Date());
                                tbUserTaskDao.insert(task);
                            }
//                            String taskId = notifyMap.get("taskId");
//                            if (tbOrders != null && taskId != null) {
//                                LocalDate startTime = LocalDate.now();
//                                int count = tbUserTaskDao.selectCount(new QueryWrapper<>(tbUserTaskDao.selectByCreateTime(tbOrders.getBuyerId(),
//                                        startTime, Long.valueOf(taskId))));
//                                if (count > 0) {
//                                    log.warn( "该用户今天已经完成过该任务");
//                                }
//                                TbUserTask task = new TbUserTask();
//                                task.setUserId(tbOrders.getBuyerId());
//                                task.setTaskId(tbOrders.getTaskId());
//                                task.setState(1);
//                                task.setCreateTime(new Date());
//                                tbUserTaskDao.insert(task);
//                                GoldRecords records = new GoldRecords();
//                                records.setUserId(tbOrders.getBuyerId());
//                                records.setGoldChange(1);
//                                records.setRemark("买到物品奖励");
//                                records.setCreateTime(new Date());
//                                records.setRelatedId(tbOrders.getOrderId());
//                                TbDailyTasks tbDailyTasks = tbDailyTasksDao.selectById(tbOrders.getTaskId());
//                                records.setGoldCoins(tbDailyTasks.getSpecies());
//                                int resultCode = goldRecordsDao.insert(records);
//                                if (resultCode <= 0) {
//                                    log.warn("买入物品失败");
//                                }
//                                UserEntity userEntity = userService.selectUserById(tbOrders.getBuyerId());
//                                userEntity.setTotalGold(userEntity.getTotalGold() + tbDailyTasks.getSpecies());
//                                userService.updateById(userEntity);
//                            }
                            payDetails.setState(1);
                            payDetails.setPayTime(DateUtils.format(new Date()));
//                            payDetails.setTradeNo("");
                            payDetailsDao.updateById(payDetails);
//                            payDetailsDao.updateState(payDetails.getId(), 1, sdf.format(new Date()), "");
                            if (payDetails.getState() == 0) {
                                if (payDetails.getType() == 1) {
                                    //余额充值
                                    userMoneyDao.updateMayMoney(1, payDetails.getUserId(), payDetails.getMoney());
                                    UserMoneyDetails userMoneyDetails = new UserMoneyDetails();
                                    userMoneyDetails.setMoney(BigDecimal.valueOf(payDetails.getMoney()));
                                    userMoneyDetails.setUserId(payDetails.getUserId());
                                    userMoneyDetails.setContent("微信充值：" + payDetails.getMoney());
                                    userMoneyDetails.setTitle("账户充值");
                                    userMoneyDetails.setType(1);
                                    userMoneyDetails.setClassify(0);
                                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                    userMoneyDetails.setCreateTime(simpleDateFormat.format(new Date()));
                                    userMoneyDetailsService.save(userMoneyDetails);
                                } else if (payDetails.getType() == 2) {
                                    //支付订单
                                    UserMoneyDetails userMoneyDetails = new UserMoneyDetails();
                                    userMoneyDetails.setMoney(BigDecimal.valueOf(payDetails.getMoney()));
                                    userMoneyDetails.setUserId(payDetails.getUserId());
                                    userMoneyDetails.setContent("支付订单金额：" + payDetails.getMoney());
                                    userMoneyDetails.setTitle("订单支付");
                                    userMoneyDetails.setType(2);
                                    userMoneyDetails.setClassify(1);
                                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                    userMoneyDetails.setCreateTime(simpleDateFormat.format(new Date()));
                                    userMoneyDetailsService.save(userMoneyDetails);
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            log.error("微信支付回调异常：" + e.getMessage(), e);
                        } finally {
                            reentrantReadWriteLock.writeLock().unlock();
                        }
                        log.error("微信手机支付回调成功订单号:" + out_trade_no + "");
                        xmlBack = "<xml>" + "<return_code><![CDATA[SUCCESS]]></return_code>" + "<return_msg><![CDATA[OK]]></return_msg>" + "</xml> ";
                    } else {
                        log.error("微信手机支付回调成功订单号:" + out_trade_no + "");
                        xmlBack = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>" + "<return_msg><![CDATA[报文为空]]></return_msg>" + "</xml> ";
                    }
                } else {
                }
                return xmlBack;
            } else {
                // 签名错误，如果数据里没有sign字段，也认为是签名错误
                log.error("手机支付回调通知签名错误");
                xmlBack = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>" + "<return_msg><![CDATA[报文为空]]></return_msg>" + "</xml> ";
                return xmlBack;
            }
        } catch (Exception e) {
            log.error("手机支付回调通知失败" + e.getMessage(), e);
            xmlBack = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>" + "<return_msg><![CDATA[报文为空]]></return_msg>" + "</xml> ";
        }
        return xmlBack;
    }

    /**
     * 计算用户上级收益
     *
     * @param orderId  订单id
     * @param classify 1订单分成 2订单退款
     */
    private void setCommission(Long orderId, int classify) {
        //计算用户上级收益
        TbOrders tbOrders = tbOrdersDao.selectById(orderId);
        UserEntity user = userService.queryByUserId(tbOrders.getBuyerId());
        // 查他的上级
        UserEntity userParent = userService.getOne(new QueryWrapper<UserEntity>().eq("inviter_code", user.getInviterCode()));
        if (userParent != null) {
            String divideScale = user.getDivideScale();
            BigDecimal money = tbOrders.getTotalPrice().multiply(new BigDecimal(divideScale));
            this.setIndentMoneyDetails(userParent, tbOrders, money, 1, classify);
            // 查他的上上级
            UserEntity userParentTwo = userService.getOne(new QueryWrapper<UserEntity>().eq("inviter_code", userParent.getInviterCode()));
            if (userParentTwo != null) {
                String divideTwoScale = userParent.getDivideTwoScale();
                BigDecimal moneyTwo = tbOrders.getTotalPrice().multiply(new BigDecimal(divideTwoScale));
                this.setIndentMoneyDetails(userParentTwo, tbOrders, moneyTwo, 2, classify);
            }
        }
    }


    /**
     * @param userEntity 用户id
     * @param tbOrders   订单
     * @param type       1一级用户 2二级用户
     * @param classify   1订单分成 2订单退款
     */
    private void setIndentMoneyDetails(UserEntity userEntity, TbOrders tbOrders, BigDecimal money, int type, int classify) {
        String title;
        //增加余额
        BigDecimal moneyDetails;
        if (classify == 1) {
            moneyDetails = userEntity.getBalance().add(money);
            title = "分成";
        } else {
            moneyDetails = userEntity.getBalance().subtract(money);
            title = "退款";
        }
        tbOrdersDao.updateRiderBalance(money, userEntity.getUserId());
        //添加用户邀请人钱包详情
        UserMoneyDetails userMoneyDetails = new UserMoneyDetails();
        userMoneyDetails.setUserId(userEntity.getUserId());
        if (type == 1) {
            userMoneyDetails.setMoney(money);
            userMoneyDetails.setUserLevel(1);
            userMoneyDetails.setRelationId(userEntity.getUserId());
            userMoneyDetails.setType(1);
            userMoneyDetails.setTitle("一级用户订单:" + tbOrders.getOrderNumber());
            userMoneyDetails.setContent("订单号:" + tbOrders.getOrderNumber() + ",订单总价:" + tbOrders.getTotalPrice() + "," + title + "金额:" + money);
            userMoneyDetails.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            userMoneyDetails.setClassify(2);
            userMoneyDetailsService.save(userMoneyDetails);
        } else if (type == 2) {
            userMoneyDetails.setMoney(money);
            userMoneyDetails.setRelationId(userEntity.getUserId());
            userMoneyDetails.setUserLevel(2);
            userMoneyDetails.setType(1);
            userMoneyDetails.setTitle("二级用户订单:" + tbOrders.getOrderNumber());
            userMoneyDetails.setContent("订单号:" + tbOrders.getOrderNumber() + ",订单总价:" + tbOrders.getTotalPrice() + "," + title + "金额:" + money);
            userMoneyDetails.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            userMoneyDetails.setClassify(2);
            userMoneyDetailsService.save(userMoneyDetails);

        }


        //app端用户消息推送
        userService.pushToSingle("订单分成", "订单号:" + tbOrders.getOrderNumber() + ",订单总价:" + tbOrders.getTotalPrice() + "," + title + "金额:" + money, userEntity.getClientid());

    }

    public String getGeneralOrder() {
        Date date = new Date();
        String newString = String.format("%0" + 4 + "d", (int) ((Math.random() * 9 + 1) * 1000));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String format = sdf.format(date);
        return format + newString;
    }

    @Override
    public Result payMoney(Long userId, BigDecimal money, Long parentId, Integer classify) throws Exception {
        String generalOrder = getGeneralOrder();
        PayDetails payDetails = new PayDetails();
        payDetails.setState(0);
        payDetails.setCreateTime(sdf.format(new Date()));
        payDetails.setOrderId(generalOrder);
        payDetails.setUserId(userId);
        payDetails.setParentId(parentId);
        payDetails.setMoney(money.doubleValue());
        payDetails.setClassify(classify);
        payDetails.setType(1);
        payDetailsDao.insert(payDetails);
        if (classify == 7) {
            return payH5(money.doubleValue(), classify, userId, generalOrder);
        }
        return pay(money.doubleValue(), classify, userId, generalOrder);
    }


    @Override
    public Result payOrder(Long userId, Long orderId, Integer classify) throws Exception {
        TbOrders orders = tbOrdersDao.selectOne(new LambdaQueryWrapper<TbOrders>().eq(TbOrders::getOrderId, orderId));
        if (orders == null) {
            return Result.error("订单不存在");
        }
        if (orders.getStatus() != 0) {
            return Result.error("订单状态已变更,请刷新后重试");
        }
        Integer count = goodsDao.countByGoodId(orders.getGoodId());
        if (count > 0) {
            return Result.error("该商品已被买走或售罄，请选择其他商品");
        }
        Goods goods = goodsDao.selectById(orders.getGoodId());
        if (goods.getIsEnable() != 1 || goods.getStatus() != 1) {
            return Result.error("该商品已下架或状态异常，请刷新后重试！");
        }
        PayDetails payDetails = payDetailsDao.selectOne(new QueryWrapper<PayDetails>().eq("order_id", orders.getOrderNumber()));
        if (payDetails == null) {
            payDetails = new PayDetails();
            payDetails.setState(0);
            payDetails.setCreateTime(sdf.format(new Date()));
            payDetails.setOrderId(orders.getOrderNumber());
            payDetails.setUserId(userId);
            payDetails.setMoney(orders.getTotalPrice().doubleValue());
            payDetails.setClassify(classify);
            payDetails.setType(2);
            payDetailsDao.insert(payDetails);
        } else {
            payDetails.setClassify(classify);
            payDetailsDao.updateById(payDetails);
        }
        if (classify == 7) {
            return payH5(payDetails.getMoney(), classify, userId, payDetails.getOrderId());
        }
        return pay(payDetails.getMoney(), classify, userId, payDetails.getOrderId());
    }

    @Override
    public boolean refund(String ordersNo, BigDecimal refundMoney) {
        WXConfigUtil config = null;
        String h5Url = commonInfoService.findOne(19).getValue().split("://")[1];
        String filePath = "/www/wwwroot/" + h5Url + "/service/apiclient_cert.p12";
        try {
            config = new WXConfigUtil(filePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int commInfoId = 0;
        PayDetails payDetails = payDetailsDao.selectByOrderId(ordersNo);
        if (refundMoney == null) {
            refundMoney = BigDecimal.valueOf(payDetails.getMoney());
        }
        Integer payWay = payDetails.getClassify();
        //orders.getPayWay(); //1app微信 2微信公众号 3微信小程序
        switch (payWay) {
            case 1:
                commInfoId = 74;
                break; //appId
            case 2:
                commInfoId = 5;
                break; //公众号id
            case 3:
                commInfoId = 45;
                break; //小程序id
            default:
                return false;
        }
        config.setAppId(commonInfoService.findOne(commInfoId).getValue());
        config.setKey(commonInfoService.findOne(75).getValue());
        config.setMchId(commonInfoService.findOne(76).getValue());
        WXPay wxpay = new WXPay(config);
        Map<String, String> data = new HashMap<>();
        data.put("appid", config.getAppID());
        data.put("mch_id", config.getMchID());
        data.put("nonce_str", WXPayUtil.generateNonceStr());
        try {
            data.put("sign", WXPayUtil.generateSignature(data, config.getKey(), WXPayConstants.SignType.MD5));
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        data.put("out_trade_no", payDetails.getOrderId()); //订单号,支付单号一致
        data.put("out_refund_no", getGeneralOrder()); //退款单号，同一笔用不同的退款单号
        BigDecimal multiply = refundMoney.multiply(BigDecimal.valueOf(100));
        String fee = String.valueOf(multiply.intValue());
        String total = String.valueOf(BigDecimal.valueOf(payDetails.getMoney()).multiply(BigDecimal.valueOf(100)).intValue());
        data.put("total_fee", total); //1块等于微信支付传入100);
        data.put("refund_fee", fee); //1块等于微信支付传入100);
        //使用官方API退款
        try {
            Map<String, String> response = wxpay.refund(data);
            if ("SUCCESS".equals(response.get("return_code"))) {//主要返回以下5个参数
                System.err.println("退款成功");
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            log.info("微信退款异常：" + e.getMessage(), e);
            e.printStackTrace();
            return false;
        }
    }

}